<?PHP
class User{
  public $name;
  public $role;

  function __construct(){
    $this->role = new Role();
	$this->role->load(1);
  }
  
  function login($name,$password){
    $password = $GLOBALS['db']->EscapeString(trim($password));
	if($this->checkPassword($password,trim($name))){
	  $this->load(trim($name));
      return true;
    }
    else{
      return false;
    }
  }

  function checkPassword($password,$name){
    $password =$GLOBALS['db']->EscapeString(trim($password));
    $name = $GLOBALS['db']->EscapeString(trim($name));
	$count = $GLOBALS['db']->ReadField("SELECT COUNT(*) FROM {'dbprefix'}user WHERE 
                        name='".$name."' AND password = '".md5($password)."'");
    return $count == 1;
  }
  
  function logout(){
	 session_destroy();
  }
  
  function getAllUser(){
    $users = $GLOBALS['db']->ReadRows("SELECT * FROM {'dbprefix'}user ORDER by name");
	foreach($users as $user){
	  $newUser = new User();
	  $newUser->id   = $user->id;
	  $newUser->name = $user->name;
	  $newUser->role->load($user->role);
	  $res[] = $newUser;
	}
	return $res;
  }

  function load($name){
	$name = $GLOBALS['db']->EscapeString(trim($name));
	$user = $GLOBALS['db']->ReadRow("SELECT * FROM {'dbprefix'}user WHERE name = '".$name."'");
	$this->id   = $user->id;
	$this->name = $user->name;
	$this->role->load($user->role);
  }

  function isGuest(){
		return $this->role->ID == 1;
  }
}
?>